package com.zz.hhbl.ui.operationdemonstration.banner

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.zz.framework.core.ext.dp
import com.zz.framework.core.ext.loadUrl
import com.zz.framework.widget.banner.adapter.BannerAdapter
import com.zz.hhbl.databinding.ItemVideoLayoutBinding
import com.zz.hhbl.entity.SpotConfigEntity

/**
 * 自定义布局，图片
 */
class DemonstrationAdapter(mDatas: List<SpotConfigEntity?>?) : BannerAdapter<SpotConfigEntity?, RecyclerView.ViewHolder>(mDatas) {
    //更新数据
    fun updateData(data: List<SpotConfigEntity?>) {
        //这里的代码自己发挥，比如如下的写法等等
        mDatas.clear()
        mDatas.addAll(data)
        notifyDataSetChanged()
    }


    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 2) {
            return VideoHolder(ItemVideoLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false).root)
        } else {
            val imageView = ImageView(parent.context)
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            val params = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            imageView.layoutParams = params
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            return ImageHolder(imageView)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (mDatas[position]!!.type.orEmpty()) {
            "image" -> 1
            "video" -> 2
            else -> super.getItemViewType(position)
        }

    }

    override fun onBindView(holder: RecyclerView.ViewHolder?, data: SpotConfigEntity?, position: Int, size: Int) {
        when (holder) {
            is ImageHolder -> {
                holder?.imageView?.loadUrl(data?.url,roundRadius = 9.dp())
            }
            is VideoHolder -> {
                holder?.onBind(position,data)
            }
        }
    }
}

